{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Autokeras_Titanic.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iGEzMrP7DzOO",
        "colab_type": "text"
      },
      "source": [
        "First install autokeras from pip"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "eY35wv7Uyvbz",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!pip install autokeras"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jSoV8IOlFlke",
        "colab_type": "text"
      },
      "source": [
        "Now install Tensorflow GPU"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "C3GQpVLrFpCM",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!pip install tensorflow-gpu==2.1.0"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4PCynrASD_hA",
        "colab_type": "text"
      },
      "source": [
        "import autokeras"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1IMvNhwYynLx",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np\n",
        "import tensorflow as tf\n",
        "\n",
        "import autokeras as ak"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "TdSaPDJCECTQ",
        "colab_type": "text"
      },
      "source": [
        "Prepare the dataset.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "grv77LqxzIqG",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def imdb_raw():\n",
        "    max_features = 20000\n",
        "    index_offset = 3  # word index offset\n",
        "\n",
        "    (x_train, y_train), (x_test, y_test) = tf.keras.datasets.imdb.load_data(\n",
        "        num_words=max_features,\n",
        "        index_from=index_offset)\n",
        "    x_train = x_train\n",
        "    y_train = y_train.reshape(-1, 1)\n",
        "    x_test = x_test\n",
        "    y_test = y_test.reshape(-1, 1)\n",
        "\n",
        "    word_to_id = tf.keras.datasets.imdb.get_word_index()\n",
        "    word_to_id = {k: (v + index_offset) for k, v in word_to_id.items()}\n",
        "    word_to_id[\"<PAD>\"] = 0\n",
        "    word_to_id[\"<START>\"] = 1\n",
        "    word_to_id[\"<UNK>\"] = 2\n",
        "\n",
        "    id_to_word = {value: key for key, value in word_to_id.items()}\n",
        "    x_train = list(map(lambda sentence: ' '.join(\n",
        "        id_to_word[i] for i in sentence), x_train))\n",
        "    x_test = list(map(lambda sentence: ' '.join(\n",
        "        id_to_word[i] for i in sentence), x_test))\n",
        "    x_train = np.array(x_train, dtype=np.str)\n",
        "    x_test = np.array(x_test, dtype=np.str)\n",
        "    return (x_train, y_train), (x_test, y_test)\n",
        "\n",
        "\n",
        "# Prepare the data.\n",
        "(x_train, y_train), (x_test, y_test) = imdb_raw()\n",
        "print(x_train.shape)  # (25000,)\n",
        "print(y_train.shape)  # (25000, 1)\n",
        "print(x_train[0][:50])  # <START> this film was just brilliant casting <UNK>"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CB2E2iYqLAfO",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "clf = ak.TextClassifier(max_trials=3)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sB-dvwNrEHTf",
        "colab_type": "text"
      },
      "source": [
        "fit dataset to model"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LtCkQMa6DlJY",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "clf.fit(x_train, y_train, epochs=2)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7davBFSIEKJG",
        "colab_type": "text"
      },
      "source": [
        "get accuracy from trained model"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yjxyIzarDofC",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print('Accuracy: {accuracy}'.format(accuracy=clf.evaluate(x_test, y_test)))"
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}